import Vue from 'vue';
import GlobalDialog from "@common/components/dialog/global";
import Printer from "./Printer";

const instance = new Vue({
	data() {
		return {
			disabledConfirmBtn: true,
		};
	},
});
const ChoosePrinter = (store, router) => {
	let dialog = GlobalDialog(store, router);
	return (options = {}) => {
		return new Promise(resolve => {
			dialog({
				title: '选择打印机',
				width: '550px',
				component: {
					value: Printer,
					props: {
						template: options.template,
						data: options.data,
						printMethod: options.printMethod,
					},
					event(event) {
						if (event.type === 'disabledConfirmBtn') {
							instance.disabledConfirmBtn = event.value;
						}
					},
				},
				confirmBtn: {
					type: 'primary',
					text: '确认打印',
					disabledFn() {
						return instance.disabledConfirmBtn;
					},
					click(dialog, { component }) {
						if (component && typeof component.confirm === 'function') {
							component.confirm().then(data => {
								if (data) {
									resolve(data);
									dialog.visible = false;
								}
							});
						}
					},
				},
			});
		});
	};
};
export default ChoosePrinter;
